package longcaisuyun.longcai.com.fragment;

import android.app.Dialog;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.zcx.helper.http.AsyCallBack;

import longcaisuyun.longcai.com.mystatic.publicUrl;
import longcaisuyun.longcai.com.net.PostMain;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.CaoZuoActivity;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.FuWuActiviyy;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.GonggaoActivity;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.HistoryActivity;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.LoginActivity;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.MainActivity;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.MoneyActivity;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.MoreActivity;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.MyApplication;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.OwnDataActivity;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.R;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.SuYunActivity;


/**
 * Created by Administrator on 2016/2/18.
 */
public class frag_main_own extends Fragment {
    Button button;
    View v;
    String telephone;
    private Dialog dialog;
    LinearLayout lin_suyun, lin_fuwu, lin_caozuo, lin_gonggao, lin_kefu, lin_more;
    RelativeLayout rl_month, rl_yue;
    TextView tv_name, tv_mounth, tv_money;
    ImageView image_icon;
    private DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.iconmain) // 设置图片下载期间显示的图片
            .showImageForEmptyUri(R.mipmap.iconmain) // 设置图片Uri为空或是错误的时候显示的图片
            .showImageOnFail(R.mipmap.iconmain) // 设置图片加载或解码过程中发生错误显示的图片
            .cacheInMemory(false) // 设置下载的图片是否缓存在内存中
            .cacheOnDisk(true) // 设置下载的图片是否缓存在SD卡中
            .displayer(new FadeInBitmapDisplayer(100)).build(); // 构建完成
    private ImageLoader myimageloader = ImageLoader.getInstance();

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = View.inflate(getActivity(), R.layout.frag_main_own, null);
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) v);
        ViewInit();
        onclick();
        Httppost();

        return v;
    }

    private void Httppost() {
        MyApplication.myviewutil.startProgressDialog(getActivity());

        new PostMain(MyApplication.myPreferences.readUid(), new AsyCallBack<PostMain.Info>() {

            @Override
            public void onSuccess(String toast, int type, PostMain.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                if (info.message.equals("0")) {
                    Toast.makeText(getActivity(), "获取数据失败", Toast.LENGTH_SHORT).show();
                    MyApplication.myviewutil.stopProgressDialog();
                } else {
                    telephone = info.telephone;
                    tv_name.setText(info.username);
                    tv_mounth.setText(info.monthincome + "元");
                    tv_money.setText(info.balance + "元");
                    myimageloader.displayImage(publicUrl.headurl + info.picurl,
                            image_icon, options);
                    MyApplication.myviewutil.stopProgressDialog();
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                Toast.makeText(getActivity(), "获取数据失败,请检查您的网络设置后，再重新尝试", Toast.LENGTH_SHORT).show();
                MyApplication.myviewutil.stopProgressDialog();
            }
        }).execute(getActivity());
    }

    private void ViewInit() {
        image_icon = (ImageView) v.findViewById(R.id.image_icon);
        rl_month = (RelativeLayout) v.findViewById(R.id.rl_month);
        rl_yue = (RelativeLayout) v.findViewById(R.id.rl_yue);
        lin_suyun = (LinearLayout) v.findViewById(R.id.lin_suyun);
        lin_fuwu = (LinearLayout) v.findViewById(R.id.lin_fuwu);
        lin_caozuo = (LinearLayout) v.findViewById(R.id.lin_caozuo);
        lin_gonggao = (LinearLayout) v.findViewById(R.id.lin_gonggao);
        lin_kefu = (LinearLayout) v.findViewById(R.id.lin_kefu);
        lin_more = (LinearLayout) v.findViewById(R.id.lin_more);
        button = (Button) v.findViewById(R.id.button);
        tv_name = (TextView) v.findViewById(R.id.tv_name);
        tv_mounth = (TextView) v.findViewById(R.id.tv_mounth);
        tv_money = (TextView) v.findViewById(R.id.tv_money);
    }

    private void onclick() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                diag_exit();
            }
        });
        lin_fuwu.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                getActivity().startActivity(new Intent(getActivity(), FuWuActiviyy.class));
            }
        });

        lin_suyun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getActivity(), SuYunActivity.class));
            }
        });
        lin_caozuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getActivity(), CaoZuoActivity.class));
            }
        });
        lin_kefu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                diag();
            }
        });
        lin_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getActivity(), MoreActivity.class));
            }
        });
        lin_gonggao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getActivity(), GonggaoActivity.class));
            }
        });
        rl_month.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getActivity(), HistoryActivity.class));
            }
        });
        rl_yue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getActivity(), MoneyActivity.class));
            }
        });
        image_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getActivity(), OwnDataActivity.class));
            }
        });
    }

    private Exit exit;

    public interface Exit {
        void appexit();
    }

    @Override
    public void onResume() {
        Httppost();
        super.onResume();
    }

    public void exit(Exit exit) {
        this.exit = exit;
    }

    public void diag() {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View view = inflater.inflate(R.layout.item_caozuo_phone, null);
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        dialog = new Dialog(v.getContext());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
        dialog.show();
        dialog.setContentView(view);
        TextView tv_phone = (TextView) view.findViewById(R.id.t_phone);
        TextView tv_cancle = (TextView) view.findViewById(R.id.t_cancle);
        tv_phone.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telephone));
//                if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
//                    return;
//                }
                getActivity().startActivity(intent);
                dialog.dismiss();
            }
        });
        tv_cancle.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    public void diag_exit() {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View layout = inflater.inflate(R.layout.item_exit,
                null);
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) layout);
        dialog = new Dialog(v.getContext());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
        dialog.show();
        dialog.setContentView(layout);
        TextView tv_phone = (TextView) layout.findViewById(R.id.t_phone);
        TextView tv_cancle = (TextView) layout.findViewById(R.id.t_cancle);
        tv_phone.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
//                exit.appexit();
                dialog.dismiss();
                MyApplication.myPreferences.clearAll();
                MainActivity.tuiChu.tuiChu();
//                MyApplication.getInstance().finishActivity();
                Intent intent = new Intent(getActivity().getApplicationContext(), LoginActivity.class);
                startActivity(intent);
            }
        });
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
}
